<?php

//就近 www.jiujin.me
global $_W, $_GPC;
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
//开通小店店铺
if ($operation == 'display') {
    ca('order.view');
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $params[':uniacid'] = $_W['uniacid'];
    $condition = '';
    $paytype = array('cash' => array('css' => 'default', 'name' => '余额支付'),'wechat' => array('css' => 'success', 'name' => '微信支付'), 'unionpay' => array('css' => 'warning', 'name' => '银联支付')); //支付方式
    $starttime = empty($_GPC['time']['start']) ? strtotime('-1 month') : strtotime($_GPC['time']['start']);
    $endtime = empty($_GPC['time']['end']) ? TIMESTAMP : strtotime($_GPC['time']['end']);
    if($_GPC['searchtime']==1){
        $condition .= " AND  a.createtime >= :starttime AND  a.createtime <= :endtime ";
        $params[':starttime'] = $starttime;
        $params[':endtime'] = $endtime;
    }
    if (!empty($_GPC['member'])) {
        $_GPC['realname'] = trim($_GPC['realname']);
        $condition .= ' and ( b.realname like :member or b.nickname like :member or b.mobile like :member)';
        $params[':member'] = "%{$_GPC['member']}%";
    }
    if (!empty($_GPC['ordersn'])) {
        $condition .= 'and a.ordersn like :ordersn';
        $params[':ordersn'] = "%{$_GPC['ordersn']}%";
    }
    if (!empty($_GPC['status'])) {
        $condition .= 'and a.status=:status ';
        $params[':status'] =intval($_GPC['status']);
    }
    if (!empty($_GPC['paytype'])) {
        $condition.=" and c.type=:type ";
        $params[':type'] = trim($_GPC['paytype']);
    }
    if (!empty($_GPC['shoptype'])) {
        $condition.=" and a.type=:type ";
        $params[':type'] = trim($_GPC['shoptype']);
    }
    $list = pdo_fetchall("select a.id,a.ordersn,a.type,a.price,a.createtime,a.paytime,a.sendtime,a.paytype,a.status,a.openid,b.realname,b.nickname,b.mobile,b.avatar,c.fee,c.type as pay_type from " . tablename('ewei_shop_commission_apply_order') . " as a left join " . tablename("ewei_shop_member") . "as b on a.openid=b.openid LEFT JOIN ".tablename('ewei_shop_commission_apply_paylog')." as c on a.ordersn=c.tid where a.uniacid=:uniacid and a.status>0  {$condition}  order by a.id desc limit " . ($pindex - 1) * $psize . ',' . $psize, $params);
    $total = pdo_fetchcolumn("select count(*) from " . tablename('ewei_shop_commission_apply_order') . " as a left join " . tablename("ewei_shop_member") . "as b on a.openid=b.openid LEFT JOIN ".tablename('ewei_shop_commission_apply_paylog')." as c on a.ordersn=c.tid where a.uniacid=:uniacid and a.status>0  {$condition}  order by a.id desc ", $params);
    $pager = pagination($total, $pindex, $psize);
    //审核开通
   if ($_GPC['open']) {
       ca('order.op.send');
       $data = pdo_fetch("select a.id as recordid,b.id,a.ordersn,a.openid,a.type,a.price,a.createtime,a.paytime,a.sendtime,a.paytype,a.status,a.openid,b.realname,b.nickname,b.mobile,b.avatar,b.weixin,b.openid,b.isShop from " . tablename('ewei_shop_commission_apply_order') . "as a  left join  " . tablename('ewei_shop_member') . "as b on a.openid=b.openid where a.uniacid=:uniacid  and a.id=:id  ", array(':uniacid' => $_W['uniacid'], ':id' => $_GPC['open']));
       $user = pdo_fetch("select * from " . tablename('ewei_shop_agent') . "  where uniacid=:uniacid  and mid=:mid  ", array(':uniacid' => $_W['uniacid'], ':mid' => $data['id']));
       if ($user) {
           if ($data['isShop'] == 0) {
               pdo_update("ewei_shop_member", array("isShop" =>1,'shoptype'=>$data['type'],'agenttime'=>time()), array('uniacid' => $_W['uniacid'], 'openid' => $data['openid']));
               pdo_update("ewei_shop_agent", array("opentime" =>time()), array('uniacid' => $_W['uniacid'], 'mid' => $data['id']));
           }
       }else{
           $shop = array(
               'uniacid' => $_W['uniacid'],
               'mid' => $data['id'],
               'createtime' => time(),
               'opentime' => time()
           );
           pdo_update("ewei_shop_member", array("isShop" =>1,'shoptype'=>$data['type'],'agenttime'=>time()), array('uniacid' => $_W['uniacid'], 'openid' => $data['openid']));
           pdo_insert("ewei_shop_agent", $shop);
       }
       $xiaoshop = pdo_fetch('select * from ' . tablename('ewei_shop_commission_shop') . ' where uniacid=:uniacid and mid=:mid limit 1', array(':uniacid' => $_W['uniacid'], ':mid' => $data['id']));
       $shopdata['selectgoods'] = 1;
       $shopdata['selectcategory'] = 0;
       $shopdata['uniacid'] = $_W['uniacid'];
       $shopdata['mid'] = $data['id'];
       if (empty($xiaoshop['id'])) {
           pdo_insert('ewei_shop_commission_shop', $shopdata);
       } else {
           pdo_update('ewei_shop_commission_shop', $shopdata, array('id' => $xiaoshop['id']));
       }
       if(!empty($data['id'])){
       pdo_update('ewei_shop_member', array('isagent' => 1, 'status' =>1,'agentlevel'=>3,"dongfan"=>0), array('id' => $data['id'], 'uniacid' => $_W['uniacid']));
       pdo_update("ewei_shop_commission_apply_order", array('sendtime' => time(), 'status' => 2), array('id' => $_GPC['open']));
       }
       if ($data['type'] == 1) {
           $data_type = "银冠店";
       }
       if ($data['type'] == 2) {
           $data_type = "金冠店";
       }
       if ($data['type'] == 3) {
           $data_type = "钻石店";
       }
       $msg = array(
           'first' => array('value' => "云店审核通过通知！", "color" => "#4a5077"),
           'username' => array('title' => '申请人', 'value' => $data['nickname'], "color" => "#4a5077"),
           'orderProductPrice' => array('title' => '支付金额', 'value' => '￥' . $data['price'] . '元', "color" => "#4a5077"),
           'orderProductName' => array('title' => '店铺类型', 'value' => $data_type, "color" => "#4a5077"),
           'paytime' => array('title' => '审核时间', 'value' => date('Y-m-d H:i:s', time()), "color" => "#4a5077"),
           'remark' => array('value' => "\r\n恭喜您的云店申请已审核通过，专属功能请在“个人中心”——“分销中心”中查看!", "color" => "#4a5077"),
            'remark2' => array('value' => "添加小妖客服微信号：lanxuevip2016  提供一对一咨询服务！", "color" => "#4a5077")
       );
       m('message')->sendCustomNotice($data['openid'], $msg);
       message('审核成功!',referer(), 'success');
   }
   //导出列表
    if($_GPC['export']==1){
        ca('order.op.export');
        $columns = array(
        array('title' => '编号', 'field' => 'ordersn', 'width' => 24),
        array('title' => '呢称', 'field' => 'nickname', 'width' => 24), 
        array('title' => '店铺类型', 'field' => 'type', 'width' => 24),
        array('title' => '支付类型', 'field' => 'pay_type', 'width' => 24),
        array('title' => '支付金额', 'field' => 'fee', 'width' => 24),
        array('title' => '姓名', 'field' => 'realname', 'width' => 24),
        array('title' => '手机号码', 'field' => 'mobile', 'width' => 24),
        array('title' => '付款时间', 'field' => 'createtime', 'width' => 24),
        array('title' => '审核时间', 'field' => 'sendtime', 'width' => 24),
        array('title' => '状态', 'field' => 'status', 'width' => 24),
        );
       $list = pdo_fetchall("select a.id,a.ordersn,a.type,a.price,a.createtime,a.paytime,a.sendtime,a.paytype,a.status,a.openid,b.realname,b.nickname,b.mobile,b.avatar,c.fee,c.type as pay_type from " . tablename('ewei_shop_commission_apply_order') . " as a left join " . tablename("ewei_shop_member") . "as b on a.openid=b.openid LEFT JOIN ".tablename('ewei_shop_commission_apply_paylog')." as c on a.ordersn=c.tid where a.uniacid=:uniacid and a.status>0  {$condition}  order by a.id desc ", $params);
       foreach($list as $k=>$v){
         $r['ordersn']=$v['ordersn'];
         $r['nickname']=$v['nickname'];
         if($v['type']==1){
            $r['type']="银冠店"; 
         }
         if($v['type']==2){
           $r['type']="金冠店";  
         }
         if($v['type']==3){
           $r['type']="钻石店";  
         }
         if($v['pay_type']=='wechat'){
           $r['pay_type']='微信支付';
         }
         if($v['pay_type']=='unionpay'){
           $r['pay_type']='银联支付';
         }
         if($v['pay_type']=='cash'){
           $r['pay_type']='余额支付';
         }
         $r['fee']=$v['fee']."元";
         $r['realname']=$v['realname'];
         $r['mobile']=$v['mobile'];
         $r['createtime']=date('Y-m-d H:i',$v['createtime']);
         $r['sendtime']=date('Y-m-d H:i',$v['sendtime']);
         if($v['status']==1){
             $r['status']="待审核";
         }else{
             $r['status']="已审核";
         }
         $exportlist[] = $r;
       }
       unset($r);
       m('excel')->export($exportlist, array("title" => "店铺审核数据-" . date('Y-m-d-H-i', time()), "columns" => $columns));
    }
}
load()->func('tpl');
include $this->template('web/allorder/smallshop_list');